{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "63a40136",
   "metadata": {},
   "source": [
    "### Enrichment testing and multiple testing correction\n",
    "\n",
    "* SNV, indel and SV across z-score cutoffs and allele frequency bins (3 x 4 x 5 = 60)\n",
    "* SNV, indel and SV window tests across z-score cutoffs (3 x 11 x 5 = 165)\n",
    "* DEL, DUP, INV, MEI rare z-score cutoffs (4 x 5 = 20) \n",
    "* DEL, DUP, INV, MEI windows (4 * 5 * 11 = 220)\n",
    "* SV consequences by z-cutoffs (47 * 5 = 235), only includes consequences actually seen in the dataset "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "45c27d1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "from pathlib import Path \n",
    "import statsmodels.api as sm\n",
    "from scipy.stats import fisher_exact\n",
    "from statsmodels.stats import multitest\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "58fb9b5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "wkdir=\"/lustre/scratch126/humgen/projects/interval_rna/interval_rna_seq/thomasVDS/misexpression_v3\"\n",
    "wkdir_path = Path(wkdir)\n",
    "\n",
    "outdir_path = wkdir_path.joinpath(\"4_vrnt_enrich/enrich_results_mul_test\")\n",
    "outdir_path.mkdir(parents=True, exist_ok=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c5b13183",
   "metadata": {},
   "outputs": [],
   "source": [
    "all_carrier_count_path = wkdir_path.joinpath(\"4_vrnt_enrich/combine_count_carriers/snp_indel_sv_all_carrier_count_z_cutoff.tsv\")\n",
    "all_carrier_count_df = pd.read_csv(all_carrier_count_path, sep=\"\\t\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "94d6751c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of tests: 60\n"
     ]
    }
   ],
   "source": [
    "# SNV, indel, SV across z-score cutoffs and AF bins\n",
    "z_cutoff_bins = [2, 10, 20, 30, 40]\n",
    "snv_indel_sv_zscore_af_df = all_carrier_count_df[all_carrier_count_df.vrnt_type.isin([\"all_sv\", \"snp\", \"indel\"]) & \n",
    "                                                  (all_carrier_count_df.consequence == \"all\") & \n",
    "                                                  (all_carrier_count_df.z_cutoff.isin(z_cutoff_bins)) & \n",
    "                                                  (all_carrier_count_df.window_name.isin([\"gene body +/-10kb\"]))\n",
    "                                                 ]\n",
    "print(f\"Number of tests: {snv_indel_sv_zscore_af_df.shape[0]}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cb4eed0e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of tests: 165\n"
     ]
    }
   ],
   "source": [
    "# Rare SNV, indel, SV across z-score cutoffs and AF bins\n",
    "z_cutoff_bins = [2, 10, 20, 30, 40]\n",
    "snv_indel_sv_rare_windows_zscore_df = all_carrier_count_df[all_carrier_count_df.vrnt_type.isin([\"all_sv\", \"snp\", \"indel\"]) & \n",
    "                                                            (all_carrier_count_df.consequence == \"all\") & \n",
    "                                                            (all_carrier_count_df.maf_range == \"0-1\") &\n",
    "                                                            (all_carrier_count_df.z_cutoff.isin(z_cutoff_bins)) & \n",
    "                                                            ~(all_carrier_count_df.window_name.isin([\"gene body +/-10kb\", \"gene body +/-200kb\"]))\n",
    "                                                 ]\n",
    "print(f\"Number of tests: {snv_indel_sv_rare_windows_zscore_df.shape[0]}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0718e57d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of tests: 20\n"
     ]
    }
   ],
   "source": [
    "# rare DEL, DUP, INV, MEI SV z-score cutoffs \n",
    "sv_types_rare_zscore_df = all_carrier_count_df[all_carrier_count_df.vrnt_type.isin([\"DEL\", \"DUP\", \"INV\", \"MEI\"]) &\n",
    "                                                (all_carrier_count_df.consequence == \"all\") & \n",
    "                                                (all_carrier_count_df.z_cutoff.isin(z_cutoff_bins)) & \n",
    "                                                (all_carrier_count_df.maf_range == \"0-1\") &\n",
    "                                                (all_carrier_count_df.window_name.isin([\"gene body +/-200kb\"]))]\n",
    "print(f\"Number of tests: {sv_types_rare_zscore_df.shape[0]}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3f2d9e70",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of tests: 220\n"
     ]
    }
   ],
   "source": [
    "# rare DEL, DUP, INV, MEI SV z-score cutoffs, windows  \n",
    "sv_types_rare_windows_zscore_df = all_carrier_count_df[all_carrier_count_df.vrnt_type.isin([\"DEL\", \"DUP\", \"INV\", \"MEI\"]) &\n",
    "                                                (all_carrier_count_df.consequence == \"all\") & \n",
    "                                                (all_carrier_count_df.z_cutoff.isin(z_cutoff_bins)) & \n",
    "                                                (all_carrier_count_df.maf_range == \"0-1\") &\n",
    "                                                ~(all_carrier_count_df.window_name.isin([\"gene body +/-10kb\", \"gene body +/-200kb\"]))]\n",
    "print(f\"Number of tests: {sv_types_rare_windows_zscore_df.shape[0]}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a54a7b56",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SV consequences with carriers: 47\n"
     ]
    }
   ],
   "source": [
    "# remove consequences never seen across the dataset \n",
    "sv_msc_zscore_2_df = all_carrier_count_df[all_carrier_count_df.vrnt_type.isin([\"DEL\", \"DUP\", \"INV\", \"MEI\"]) &\n",
    "                                         (all_carrier_count_df.consequence != \"all\") & \n",
    "                                         (all_carrier_count_df.z_cutoff == 2) & \n",
    "                                         (all_carrier_count_df.maf_range == \"0-1\") & \n",
    "                                         (all_carrier_count_df.window_name == \"gene body +/-200kb\")\n",
    "                                         ].copy()\n",
    "sv_msc_zscore_2_df[\"total_carrier\"] = sv_msc_zscore_2_df.misexp_carrier + sv_msc_zscore_2_df.control_carrier\n",
    "sv_msc_zscore_2_total_carriers_df = sv_msc_zscore_2_df[[\"vrnt_type\", \"consequence\", \"total_carrier\"]]\n",
    "sv_msc_nonzero_carrier_df = sv_msc_zscore_2_total_carriers_df[sv_msc_zscore_2_total_carriers_df.total_carrier != 0].drop_duplicates()\n",
    "sv_msc_nonzero_carrier_df = sv_msc_nonzero_carrier_df.drop(columns=[\"total_carrier\"])\n",
    "print(f\"SV consequences with carriers: {sv_msc_nonzero_carrier_df.shape[0]}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9d8fc99e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of tests: 235\n"
     ]
    }
   ],
   "source": [
    "sv_msc_zscore_df = all_carrier_count_df[all_carrier_count_df.vrnt_type.isin([\"DEL\", \"DUP\", \"INV\", \"MEI\"]) &\n",
    "                                            (all_carrier_count_df.consequence != \"all\") & \n",
    "                                            (all_carrier_count_df.z_cutoff.isin(z_cutoff_bins)) & \n",
    "                                            (all_carrier_count_df.maf_range == \"0-1\") & \n",
    "                                            (all_carrier_count_df.window_name == \"gene body +/-200kb\") \n",
    "                                       ].drop_duplicates()\n",
    "\n",
    "\n",
    "sv_msc_zscore_2_nonzero_carrier_df = pd.merge(sv_msc_zscore_df, \n",
    "                                                    sv_msc_nonzero_carrier_df, \n",
    "                                                    on=[\"vrnt_type\", \"consequence\"], \n",
    "                                                    how=\"inner\")\n",
    "print(f\"Number of tests: {sv_msc_zscore_2_nonzero_carrier_df.shape[0]}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e10bf4f9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total number of tests required: 700\n"
     ]
    }
   ],
   "source": [
    "all_carrier_count_df_list = [snv_indel_sv_zscore_af_df, \n",
    "                            snv_indel_sv_rare_windows_zscore_df,\n",
    "                            sv_types_rare_zscore_df,\n",
    "                            sv_types_rare_windows_zscore_df, \n",
    "                            sv_msc_zscore_2_nonzero_carrier_df\n",
    "                           ]\n",
    "all_carrier_count_df = pd.concat(all_carrier_count_df_list).drop(columns=[\"consequence_name\"])\n",
    "test_num = all_carrier_count_df.shape[0]\n",
    "print(f\"Total number of tests required: {all_carrier_count_df.shape[0]}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4a90ce37",
   "metadata": {},
   "outputs": [],
   "source": [
    "### enrichment testing \n",
    "def enrichment_test(row):\n",
    "    \"\"\"Enrichment testing\"\"\"\n",
    "    misexp_carrier = row[\"misexp_carrier\"]\n",
    "    misexp_total = row[\"misexp_total\"]\n",
    "    control_carrier = row[\"control_carrier\"]\n",
    "    control_total =  row[\"control_total\"]\n",
    "    # contingency matrix \n",
    "    conting_mtx_list = [[misexp_carrier, misexp_total - misexp_carrier], [control_carrier, control_total - control_carrier]]\n",
    "    conting_mtx = np.array(conting_mtx_list)\n",
    "    # enrichment testing \n",
    "    oddsratio = sm.stats.Table2x2(conting_mtx).oddsratio\n",
    "    riskratio = sm.stats.Table2x2(conting_mtx).riskratio\n",
    "    _, pval = fisher_exact(conting_mtx)\n",
    "    # 95% confidence intervals by a normal approximation \n",
    "    riskratio_confint_lower,  riskratio_confint_upper = sm.stats.Table2x2(conting_mtx).riskratio_confint(0.05, method=\"normal\")\n",
    "    oddsratio_confint_lower,  oddsratio_confint_upper = sm.stats.Table2x2(conting_mtx).oddsratio_confint(0.05, method=\"normal\")\n",
    "    return pd.Series({'risk_ratio': riskratio, 'risk_ratio_lower':riskratio_confint_lower, \n",
    "                      'risk_ratio_upper': riskratio_confint_upper, 'odds_ratio':oddsratio, \n",
    "                      'odds_ratio_lower': oddsratio_confint_lower, 'odds_ratio_upper':oddsratio_confint_upper, \n",
    "                      'pval': pval})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "88ecb416",
   "metadata": {},
   "outputs": [],
   "source": [
    "enrichment_results_df = all_carrier_count_df.apply(enrichment_test, axis=1, result_type=\"expand\")\n",
    "all_enrich_tests_df = pd.concat([all_carrier_count_df, enrichment_results_df], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "060b957b",
   "metadata": {},
   "outputs": [],
   "source": [
    "### multiple testing correction \n",
    "# multiple testing correction (BH FDR method)\n",
    "pval_as_array = all_enrich_tests_df.pval.to_numpy()\n",
    "for method in [\"fdr_bh\", \"bonferroni\"]: \n",
    "    pass_test, pval_adj, _, _ = multitest.multipletests(pval_as_array, alpha=0.05, method=method)\n",
    "    all_enrich_tests_df[f\"{method}_pass\"] = pass_test\n",
    "    all_enrich_tests_df[f\"{method}_pval_adj\"] = pval_adj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f44dd899",
   "metadata": {},
   "outputs": [],
   "source": [
    "# check Bonferroni cutoff \n",
    "if 0.05/test_num < all_enrich_tests_df[all_enrich_tests_df.bonferroni_pass].pval.max(): \n",
    "    raise ValueError(\"Max p-value passing Bonferroni greater than expected cutoff.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "6b91e58e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# write to file\n",
    "all_enrich_tests_bonf_path = outdir_path.joinpath(f\"snp_indel_sv_all_enrich_results_{test_num}_bonf_adj_gene_body_10kb.tsv\")\n",
    "all_enrich_tests_df.to_csv(all_enrich_tests_bonf_path, sep=\"\\t\", index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24d13adb",
   "metadata": {},
   "source": [
    "### Enrichment Results "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "53f4f8b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "At all z-score cutoffs rare SVs are enriched in gene body +/-200kb window: True\n"
     ]
    }
   ],
   "source": [
    "# significant enrichment for rare SVs (all SVs) in gene body window +/- 200 kb\n",
    "# across all z-score cutoffs \n",
    "all_sv_rare_gene_body_200_df = all_enrich_tests_df[(all_enrich_tests_df.maf_range == \"0-1\") & \n",
    "                                                   (all_enrich_tests_df.vrnt_type == \"all_sv\") & \n",
    "                                                   (all_enrich_tests_df.window_raw == \"gene_body_10\")] \n",
    "all_sv_rare_gene_body_200 = all_sv_rare_gene_body_200_df.bonferroni_pass.all()\n",
    "print(f\"At all z-score cutoffs rare SVs are enriched in gene body +/-200kb window: {all_sv_rare_gene_body_200}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "958df73b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of tests where non-rare SVs are significantly enriched in gene body window: 1\n"
     ]
    }
   ],
   "source": [
    "# only one significant enrichment observed for low frequency and common structural variants\n",
    "# across all z-score cutoffs \n",
    "all_sv_not_rare_gene_body_200_df = all_enrich_tests_df[(all_enrich_tests_df.maf_range != \"0-1\") & \n",
    "                                                   (all_enrich_tests_df.vrnt_type == \"all_sv\") & \n",
    "                                                   (all_enrich_tests_df.window_raw == \"gene_body_10\") & \n",
    "                                                    (all_enrich_tests_df.risk_ratio > 1) \n",
    "                                                  ] \n",
    "all_sv_not_rare_gene_body_200_pass = all_sv_not_rare_gene_body_200_df[all_sv_not_rare_gene_body_200_df.bonferroni_pass].shape[0]\n",
    "print(f\"Number of tests where non-rare SVs are significantly enriched in gene body window: {all_sv_not_rare_gene_body_200_pass}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "2bfffebf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of tests where SNVs/indels (any MAF) are significantly enriched in gene body window: 0\n",
      "Max risk ratio SNVs in gene body +/- 10kb all MAF cutoffs all z-score cutoffs: 1.0379045440191041\n",
      "Max risk ratio indels in gene body +/- 10kb all MAF cutoffs all z-score cutoffs: 1.1159430987899683\n"
     ]
    }
   ],
   "source": [
    "# no significant enrichment observed for SNVs and indels at any z-score cutoff\n",
    "snv_indel_all_maf_gene_body_10_df = all_enrich_tests_df[(all_enrich_tests_df.vrnt_type.isin([\"indel\", \"snp\"])) & \n",
    "                                                        (all_enrich_tests_df.window_raw == \"gene_body_window_10000\") & \n",
    "                                                        (all_enrich_tests_df.risk_ratio > 1) \n",
    "                                                       ]\n",
    "snv_indel_all_maf_gene_body_10_pass = snv_indel_all_maf_gene_body_10_df[snv_indel_all_maf_gene_body_10_df.bonferroni_pass].shape[0]\n",
    "print(f\"Number of tests where SNVs/indels (any MAF) are significantly enriched in gene body window: {snv_indel_all_maf_gene_body_10_pass}\")\n",
    "# max indel enrichment in gene body window +/- 10 kb \n",
    "# max risk ratio  \n",
    "snv_max_nominal_enrich = all_enrich_tests_df[(all_enrich_tests_df.vrnt_type.isin([\"snp\"])) & \n",
    "                                                (all_enrich_tests_df.window_raw == \"gene_body_window_10000\")\n",
    "                                               ].risk_ratio.max()\n",
    "print(f\"Max risk ratio SNVs in gene body +/- 10kb all MAF cutoffs all z-score cutoffs: {snv_max_nominal_enrich}\")\n",
    "# max risk ratio for indels \n",
    "indel_max_nominal_enrich = all_enrich_tests_df[(all_enrich_tests_df.vrnt_type.isin([\"indel\"])) & \n",
    "                                                (all_enrich_tests_df.window_raw == \"gene_body_window_10000\")\n",
    "                                               ].risk_ratio.max()\n",
    "print(f\"Max risk ratio indels in gene body +/- 10kb all MAF cutoffs all z-score cutoffs: {indel_max_nominal_enrich}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "ef480f32",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       z_cutoff    window_name\n",
      "16769      10.0      gene body\n",
      "16779      10.0  TSS to -200kb\n",
      "16879       2.0      gene body\n",
      "16889       2.0  TSS to -200kb\n",
      "16934      20.0      gene body\n",
      "16944      20.0  TSS to -200kb\n",
      "17044      30.0      gene body\n",
      "17054      30.0  TSS to -200kb\n",
      "17154      40.0      gene body\n",
      "17164      40.0  TSS to -200kb\n"
     ]
    }
   ],
   "source": [
    "# significant enrichment for rare SVs in gene body window and upstream window \n",
    "all_sv_rare_windows_df = all_enrich_tests_df[(all_enrich_tests_df.maf_range == \"0-1\") & \n",
    "                                             (all_enrich_tests_df.vrnt_type == \"all_sv\") & \n",
    "                                             ~(all_enrich_tests_df.window_raw.isin([\"gene_body_200\", \"gene_body_10\"])) & \n",
    "                                             (all_enrich_tests_df.bonferroni_pass)\n",
    "                                            ]\n",
    "print(all_sv_rare_windows_df[[\"z_cutoff\", \"window_name\"]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "c3a5fc21",
   "metadata": {},
   "outputs": [],
   "source": [
    "# significant enrichment for rare SNVs and indels across windows\n",
    "all_snv_indel_rare_windows_df = all_enrich_tests_df[(all_enrich_tests_df.maf_range == \"0-1\") & \n",
    "                                                    (all_enrich_tests_df.vrnt_type.isin([\"indel\", \"snp\"])) & \n",
    "                                                    (all_enrich_tests_df.window_raw != \"gene_body_window_10000\") & \n",
    "                                                    (all_enrich_tests_df.bonferroni_pass) &\n",
    "                                                    (all_enrich_tests_df.risk_ratio > 1)\n",
    "                                                   ]\n",
    "# observe enrichment for indels in some windows but not consistent across windows and z-socres \n",
    "# risk ratios very low too"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "a8b0e7f1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Maximum significant rare indel enrichment across windows: 1.1090835902065395\n"
     ]
    }
   ],
   "source": [
    "# max enrichment for indels \n",
    "max_indel_enrich_windows = all_snv_indel_rare_windows_df[all_snv_indel_rare_windows_df.vrnt_type == \"indel\"].risk_ratio.max()\n",
    "print(f\"Maximum significant rare indel enrichment across windows: {max_indel_enrich_windows}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "405a0d70",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>vrnt_type</th>\n",
       "      <th>consequence</th>\n",
       "      <th>maf_range</th>\n",
       "      <th>z_cutoff</th>\n",
       "      <th>z_cutoff_name</th>\n",
       "      <th>window_raw</th>\n",
       "      <th>window_name</th>\n",
       "      <th>misexp_carrier</th>\n",
       "      <th>misexp_total</th>\n",
       "      <th>control_carrier</th>\n",
       "      <th>...</th>\n",
       "      <th>risk_ratio_lower</th>\n",
       "      <th>risk_ratio_upper</th>\n",
       "      <th>odds_ratio</th>\n",
       "      <th>odds_ratio_lower</th>\n",
       "      <th>odds_ratio_upper</th>\n",
       "      <th>pval</th>\n",
       "      <th>fdr_bh_pass</th>\n",
       "      <th>fdr_bh_pval_adj</th>\n",
       "      <th>bonferroni_pass</th>\n",
       "      <th>bonferroni_pval_adj</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>8373</th>\n",
       "      <td>DEL</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>&gt; 2</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>187</td>\n",
       "      <td>17380</td>\n",
       "      <td>80411</td>\n",
       "      <td>...</td>\n",
       "      <td>1.185375</td>\n",
       "      <td>1.576959</td>\n",
       "      <td>1.371213</td>\n",
       "      <td>1.186995</td>\n",
       "      <td>1.584021</td>\n",
       "      <td>4.326669e-05</td>\n",
       "      <td>True</td>\n",
       "      <td>3.563139e-04</td>\n",
       "      <td>True</td>\n",
       "      <td>3.028669e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9017</th>\n",
       "      <td>DEL</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&gt; 10</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>132</td>\n",
       "      <td>10668</td>\n",
       "      <td>80234</td>\n",
       "      <td>...</td>\n",
       "      <td>1.328671</td>\n",
       "      <td>1.865497</td>\n",
       "      <td>1.581563</td>\n",
       "      <td>1.331911</td>\n",
       "      <td>1.878009</td>\n",
       "      <td>9.504309e-07</td>\n",
       "      <td>True</td>\n",
       "      <td>1.108836e-05</td>\n",
       "      <td>True</td>\n",
       "      <td>6.653016e-04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16890</th>\n",
       "      <td>DEL</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>&gt; 2</td>\n",
       "      <td>upstream_200000</td>\n",
       "      <td>TSS to -200kb</td>\n",
       "      <td>108</td>\n",
       "      <td>17380</td>\n",
       "      <td>38088</td>\n",
       "      <td>...</td>\n",
       "      <td>1.380955</td>\n",
       "      <td>2.012405</td>\n",
       "      <td>1.671217</td>\n",
       "      <td>1.382783</td>\n",
       "      <td>2.019814</td>\n",
       "      <td>7.638483e-07</td>\n",
       "      <td>True</td>\n",
       "      <td>9.492433e-06</td>\n",
       "      <td>True</td>\n",
       "      <td>5.346938e-04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9339</th>\n",
       "      <td>DEL</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>20.0</td>\n",
       "      <td>&gt; 20</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>68</td>\n",
       "      <td>4622</td>\n",
       "      <td>63210</td>\n",
       "      <td>...</td>\n",
       "      <td>1.500565</td>\n",
       "      <td>2.405959</td>\n",
       "      <td>1.913519</td>\n",
       "      <td>1.505865</td>\n",
       "      <td>2.431529</td>\n",
       "      <td>1.301025e-06</td>\n",
       "      <td>True</td>\n",
       "      <td>1.445584e-05</td>\n",
       "      <td>True</td>\n",
       "      <td>9.107176e-04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16780</th>\n",
       "      <td>DEL</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&gt; 10</td>\n",
       "      <td>upstream_200000</td>\n",
       "      <td>TSS to -200kb</td>\n",
       "      <td>85</td>\n",
       "      <td>10668</td>\n",
       "      <td>38008</td>\n",
       "      <td>...</td>\n",
       "      <td>1.731307</td>\n",
       "      <td>2.645421</td>\n",
       "      <td>2.149259</td>\n",
       "      <td>1.735760</td>\n",
       "      <td>2.661262</td>\n",
       "      <td>3.145948e-10</td>\n",
       "      <td>True</td>\n",
       "      <td>8.156161e-09</td>\n",
       "      <td>True</td>\n",
       "      <td>2.202164e-07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9661</th>\n",
       "      <td>DEL</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>30.0</td>\n",
       "      <td>&gt; 30</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>38</td>\n",
       "      <td>2019</td>\n",
       "      <td>36970</td>\n",
       "      <td>...</td>\n",
       "      <td>1.770464</td>\n",
       "      <td>3.324942</td>\n",
       "      <td>2.453609</td>\n",
       "      <td>1.779645</td>\n",
       "      <td>3.382808</td>\n",
       "      <td>1.274055e-06</td>\n",
       "      <td>True</td>\n",
       "      <td>1.438449e-05</td>\n",
       "      <td>True</td>\n",
       "      <td>8.918384e-04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16945</th>\n",
       "      <td>DEL</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>20.0</td>\n",
       "      <td>&gt; 20</td>\n",
       "      <td>upstream_200000</td>\n",
       "      <td>TSS to -200kb</td>\n",
       "      <td>45</td>\n",
       "      <td>4622</td>\n",
       "      <td>29889</td>\n",
       "      <td>...</td>\n",
       "      <td>1.987850</td>\n",
       "      <td>3.557260</td>\n",
       "      <td>2.675504</td>\n",
       "      <td>1.994336</td>\n",
       "      <td>3.589326</td>\n",
       "      <td>1.045641e-08</td>\n",
       "      <td>True</td>\n",
       "      <td>1.829871e-07</td>\n",
       "      <td>True</td>\n",
       "      <td>7.319486e-06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17055</th>\n",
       "      <td>DEL</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>30.0</td>\n",
       "      <td>&gt; 30</td>\n",
       "      <td>upstream_200000</td>\n",
       "      <td>TSS to -200kb</td>\n",
       "      <td>24</td>\n",
       "      <td>2019</td>\n",
       "      <td>17514</td>\n",
       "      <td>...</td>\n",
       "      <td>2.172666</td>\n",
       "      <td>4.815724</td>\n",
       "      <td>3.261533</td>\n",
       "      <td>2.180270</td>\n",
       "      <td>4.879028</td>\n",
       "      <td>9.986397e-07</td>\n",
       "      <td>True</td>\n",
       "      <td>1.145980e-05</td>\n",
       "      <td>True</td>\n",
       "      <td>6.990478e-04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9983</th>\n",
       "      <td>DEL</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>40.0</td>\n",
       "      <td>&gt; 40</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>28</td>\n",
       "      <td>803</td>\n",
       "      <td>15706</td>\n",
       "      <td>...</td>\n",
       "      <td>3.244114</td>\n",
       "      <td>6.721260</td>\n",
       "      <td>4.802109</td>\n",
       "      <td>3.292669</td>\n",
       "      <td>7.003513</td>\n",
       "      <td>4.685060e-11</td>\n",
       "      <td>True</td>\n",
       "      <td>1.311817e-09</td>\n",
       "      <td>True</td>\n",
       "      <td>3.279542e-08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17165</th>\n",
       "      <td>DEL</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>40.0</td>\n",
       "      <td>&gt; 40</td>\n",
       "      <td>upstream_200000</td>\n",
       "      <td>TSS to -200kb</td>\n",
       "      <td>17</td>\n",
       "      <td>803</td>\n",
       "      <td>7392</td>\n",
       "      <td>...</td>\n",
       "      <td>3.761657</td>\n",
       "      <td>9.646207</td>\n",
       "      <td>6.132420</td>\n",
       "      <td>3.790784</td>\n",
       "      <td>9.920527</td>\n",
       "      <td>8.151503e-09</td>\n",
       "      <td>True</td>\n",
       "      <td>1.463090e-07</td>\n",
       "      <td>True</td>\n",
       "      <td>5.706052e-06</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17045</th>\n",
       "      <td>DEL</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>30.0</td>\n",
       "      <td>&gt; 30</td>\n",
       "      <td>upstream_0</td>\n",
       "      <td>gene body</td>\n",
       "      <td>8</td>\n",
       "      <td>2019</td>\n",
       "      <td>3053</td>\n",
       "      <td>...</td>\n",
       "      <td>3.094722</td>\n",
       "      <td>12.362535</td>\n",
       "      <td>6.205982</td>\n",
       "      <td>3.096520</td>\n",
       "      <td>12.437905</td>\n",
       "      <td>6.199339e-05</td>\n",
       "      <td>True</td>\n",
       "      <td>5.045973e-04</td>\n",
       "      <td>True</td>\n",
       "      <td>4.339537e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17155</th>\n",
       "      <td>DEL</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>40.0</td>\n",
       "      <td>&gt; 40</td>\n",
       "      <td>upstream_0</td>\n",
       "      <td>gene body</td>\n",
       "      <td>8</td>\n",
       "      <td>803</td>\n",
       "      <td>1418</td>\n",
       "      <td>...</td>\n",
       "      <td>7.401180</td>\n",
       "      <td>29.504511</td>\n",
       "      <td>14.915926</td>\n",
       "      <td>7.419088</td>\n",
       "      <td>29.988164</td>\n",
       "      <td>1.123670e-07</td>\n",
       "      <td>True</td>\n",
       "      <td>1.673551e-06</td>\n",
       "      <td>True</td>\n",
       "      <td>7.865688e-05</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>12 rows × 22 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      vrnt_type consequence maf_range  z_cutoff z_cutoff_name  \\\n",
       "8373        DEL         all       0-1       2.0           > 2   \n",
       "9017        DEL         all       0-1      10.0          > 10   \n",
       "16890       DEL         all       0-1       2.0           > 2   \n",
       "9339        DEL         all       0-1      20.0          > 20   \n",
       "16780       DEL         all       0-1      10.0          > 10   \n",
       "9661        DEL         all       0-1      30.0          > 30   \n",
       "16945       DEL         all       0-1      20.0          > 20   \n",
       "17055       DEL         all       0-1      30.0          > 30   \n",
       "9983        DEL         all       0-1      40.0          > 40   \n",
       "17165       DEL         all       0-1      40.0          > 40   \n",
       "17045       DEL         all       0-1      30.0          > 30   \n",
       "17155       DEL         all       0-1      40.0          > 40   \n",
       "\n",
       "            window_raw         window_name  misexp_carrier  misexp_total  \\\n",
       "8373     gene_body_200  gene body +/-200kb             187         17380   \n",
       "9017     gene_body_200  gene body +/-200kb             132         10668   \n",
       "16890  upstream_200000       TSS to -200kb             108         17380   \n",
       "9339     gene_body_200  gene body +/-200kb              68          4622   \n",
       "16780  upstream_200000       TSS to -200kb              85         10668   \n",
       "9661     gene_body_200  gene body +/-200kb              38          2019   \n",
       "16945  upstream_200000       TSS to -200kb              45          4622   \n",
       "17055  upstream_200000       TSS to -200kb              24          2019   \n",
       "9983     gene_body_200  gene body +/-200kb              28           803   \n",
       "17165  upstream_200000       TSS to -200kb              17           803   \n",
       "17045       upstream_0           gene body               8          2019   \n",
       "17155       upstream_0           gene body               8           803   \n",
       "\n",
       "       control_carrier  ...  risk_ratio_lower  risk_ratio_upper  odds_ratio  \\\n",
       "8373             80411  ...          1.185375          1.576959    1.371213   \n",
       "9017             80234  ...          1.328671          1.865497    1.581563   \n",
       "16890            38088  ...          1.380955          2.012405    1.671217   \n",
       "9339             63210  ...          1.500565          2.405959    1.913519   \n",
       "16780            38008  ...          1.731307          2.645421    2.149259   \n",
       "9661             36970  ...          1.770464          3.324942    2.453609   \n",
       "16945            29889  ...          1.987850          3.557260    2.675504   \n",
       "17055            17514  ...          2.172666          4.815724    3.261533   \n",
       "9983             15706  ...          3.244114          6.721260    4.802109   \n",
       "17165             7392  ...          3.761657          9.646207    6.132420   \n",
       "17045             3053  ...          3.094722         12.362535    6.205982   \n",
       "17155             1418  ...          7.401180         29.504511   14.915926   \n",
       "\n",
       "       odds_ratio_lower  odds_ratio_upper          pval  fdr_bh_pass  \\\n",
       "8373           1.186995          1.584021  4.326669e-05         True   \n",
       "9017           1.331911          1.878009  9.504309e-07         True   \n",
       "16890          1.382783          2.019814  7.638483e-07         True   \n",
       "9339           1.505865          2.431529  1.301025e-06         True   \n",
       "16780          1.735760          2.661262  3.145948e-10         True   \n",
       "9661           1.779645          3.382808  1.274055e-06         True   \n",
       "16945          1.994336          3.589326  1.045641e-08         True   \n",
       "17055          2.180270          4.879028  9.986397e-07         True   \n",
       "9983           3.292669          7.003513  4.685060e-11         True   \n",
       "17165          3.790784          9.920527  8.151503e-09         True   \n",
       "17045          3.096520         12.437905  6.199339e-05         True   \n",
       "17155          7.419088         29.988164  1.123670e-07         True   \n",
       "\n",
       "       fdr_bh_pval_adj  bonferroni_pass  bonferroni_pval_adj  \n",
       "8373      3.563139e-04             True         3.028669e-02  \n",
       "9017      1.108836e-05             True         6.653016e-04  \n",
       "16890     9.492433e-06             True         5.346938e-04  \n",
       "9339      1.445584e-05             True         9.107176e-04  \n",
       "16780     8.156161e-09             True         2.202164e-07  \n",
       "9661      1.438449e-05             True         8.918384e-04  \n",
       "16945     1.829871e-07             True         7.319486e-06  \n",
       "17055     1.145980e-05             True         6.990478e-04  \n",
       "9983      1.311817e-09             True         3.279542e-08  \n",
       "17165     1.463090e-07             True         5.706052e-06  \n",
       "17045     5.045973e-04             True         4.339537e-02  \n",
       "17155     1.673551e-06             True         7.865688e-05  \n",
       "\n",
       "[12 rows x 22 columns]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### SV type enrichment - gene body +/- 200kb and windows \n",
    "\n",
    "# 200kb window around genes only DELs and DUPs significantly enriched\n",
    "all_enrich_tests_df[all_enrich_tests_df.vrnt_type.isin([\"DEL\"]) & \n",
    "                    (all_enrich_tests_df.consequence == \"all\") &\n",
    "                    (all_enrich_tests_df.bonferroni_pass)\n",
    "                   ].sort_values(by=\"risk_ratio\")\n",
    "\n",
    "# Deletions significantly enriched in gene body + 200kb window all z-scores \n",
    "# Deletions significantly enriched in +200kb window all z-scores \n",
    "# Deletions significantly enriched in gene body window at high z-scores (30, 40)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "ba20b38a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>vrnt_type</th>\n",
       "      <th>consequence</th>\n",
       "      <th>maf_range</th>\n",
       "      <th>z_cutoff</th>\n",
       "      <th>z_cutoff_name</th>\n",
       "      <th>window_raw</th>\n",
       "      <th>window_name</th>\n",
       "      <th>misexp_carrier</th>\n",
       "      <th>misexp_total</th>\n",
       "      <th>control_carrier</th>\n",
       "      <th>...</th>\n",
       "      <th>risk_ratio_lower</th>\n",
       "      <th>risk_ratio_upper</th>\n",
       "      <th>odds_ratio</th>\n",
       "      <th>odds_ratio_lower</th>\n",
       "      <th>odds_ratio_upper</th>\n",
       "      <th>pval</th>\n",
       "      <th>fdr_bh_pass</th>\n",
       "      <th>fdr_bh_pval_adj</th>\n",
       "      <th>bonferroni_pass</th>\n",
       "      <th>bonferroni_pval_adj</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>8453</th>\n",
       "      <td>DUP</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>&gt; 2</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>36</td>\n",
       "      <td>17380</td>\n",
       "      <td>7906</td>\n",
       "      <td>...</td>\n",
       "      <td>1.930256</td>\n",
       "      <td>3.712788</td>\n",
       "      <td>2.680538</td>\n",
       "      <td>1.931458</td>\n",
       "      <td>3.720134</td>\n",
       "      <td>2.641502e-07</td>\n",
       "      <td>True</td>\n",
       "      <td>3.555869e-06</td>\n",
       "      <td>True</td>\n",
       "      <td>1.849052e-04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9097</th>\n",
       "      <td>DUP</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&gt; 10</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>26</td>\n",
       "      <td>10668</td>\n",
       "      <td>7901</td>\n",
       "      <td>...</td>\n",
       "      <td>2.143761</td>\n",
       "      <td>4.625803</td>\n",
       "      <td>3.154317</td>\n",
       "      <td>2.145324</td>\n",
       "      <td>4.637861</td>\n",
       "      <td>6.369462e-07</td>\n",
       "      <td>True</td>\n",
       "      <td>8.106589e-06</td>\n",
       "      <td>True</td>\n",
       "      <td>4.458624e-04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9419</th>\n",
       "      <td>DUP</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>20.0</td>\n",
       "      <td>&gt; 20</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>17</td>\n",
       "      <td>4622</td>\n",
       "      <td>6242</td>\n",
       "      <td>...</td>\n",
       "      <td>2.991064</td>\n",
       "      <td>7.736091</td>\n",
       "      <td>4.824383</td>\n",
       "      <td>2.994565</td>\n",
       "      <td>7.772304</td>\n",
       "      <td>2.124559e-07</td>\n",
       "      <td>True</td>\n",
       "      <td>2.974382e-06</td>\n",
       "      <td>True</td>\n",
       "      <td>1.487191e-04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9741</th>\n",
       "      <td>DUP</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>30.0</td>\n",
       "      <td>&gt; 30</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>15</td>\n",
       "      <td>2019</td>\n",
       "      <td>3807</td>\n",
       "      <td>...</td>\n",
       "      <td>5.611961</td>\n",
       "      <td>15.413631</td>\n",
       "      <td>9.362705</td>\n",
       "      <td>5.628203</td>\n",
       "      <td>15.575174</td>\n",
       "      <td>2.165972e-10</td>\n",
       "      <td>True</td>\n",
       "      <td>5.831464e-09</td>\n",
       "      <td>True</td>\n",
       "      <td>1.516181e-07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16882</th>\n",
       "      <td>DUP</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>&gt; 2</td>\n",
       "      <td>upstream_0</td>\n",
       "      <td>gene body</td>\n",
       "      <td>22</td>\n",
       "      <td>17380</td>\n",
       "      <td>964</td>\n",
       "      <td>...</td>\n",
       "      <td>8.794992</td>\n",
       "      <td>20.468198</td>\n",
       "      <td>13.432803</td>\n",
       "      <td>8.800693</td>\n",
       "      <td>20.502952</td>\n",
       "      <td>1.220390e-17</td>\n",
       "      <td>True</td>\n",
       "      <td>9.491925e-16</td>\n",
       "      <td>True</td>\n",
       "      <td>8.542733e-15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10063</th>\n",
       "      <td>DUP</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>40.0</td>\n",
       "      <td>&gt; 40</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>12</td>\n",
       "      <td>803</td>\n",
       "      <td>1680</td>\n",
       "      <td>...</td>\n",
       "      <td>10.648617</td>\n",
       "      <td>32.870975</td>\n",
       "      <td>18.977759</td>\n",
       "      <td>10.710190</td>\n",
       "      <td>33.627351</td>\n",
       "      <td>5.427455e-12</td>\n",
       "      <td>True</td>\n",
       "      <td>1.651834e-10</td>\n",
       "      <td>True</td>\n",
       "      <td>3.799219e-09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16772</th>\n",
       "      <td>DUP</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&gt; 10</td>\n",
       "      <td>upstream_0</td>\n",
       "      <td>gene body</td>\n",
       "      <td>19</td>\n",
       "      <td>10668</td>\n",
       "      <td>959</td>\n",
       "      <td>...</td>\n",
       "      <td>12.044643</td>\n",
       "      <td>29.844000</td>\n",
       "      <td>18.991483</td>\n",
       "      <td>12.055418</td>\n",
       "      <td>29.918202</td>\n",
       "      <td>3.894176e-18</td>\n",
       "      <td>True</td>\n",
       "      <td>4.543206e-16</td>\n",
       "      <td>True</td>\n",
       "      <td>2.725923e-15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16937</th>\n",
       "      <td>DUP</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>20.0</td>\n",
       "      <td>&gt; 20</td>\n",
       "      <td>upstream_0</td>\n",
       "      <td>gene body</td>\n",
       "      <td>13</td>\n",
       "      <td>4622</td>\n",
       "      <td>772</td>\n",
       "      <td>...</td>\n",
       "      <td>17.204574</td>\n",
       "      <td>51.416810</td>\n",
       "      <td>29.823368</td>\n",
       "      <td>17.225280</td>\n",
       "      <td>51.635345</td>\n",
       "      <td>2.500025e-15</td>\n",
       "      <td>True</td>\n",
       "      <td>1.166678e-13</td>\n",
       "      <td>True</td>\n",
       "      <td>1.750018e-12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17047</th>\n",
       "      <td>DUP</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>30.0</td>\n",
       "      <td>&gt; 30</td>\n",
       "      <td>upstream_0</td>\n",
       "      <td>gene body</td>\n",
       "      <td>13</td>\n",
       "      <td>2019</td>\n",
       "      <td>496</td>\n",
       "      <td>...</td>\n",
       "      <td>35.732239</td>\n",
       "      <td>107.118858</td>\n",
       "      <td>62.262029</td>\n",
       "      <td>35.835576</td>\n",
       "      <td>108.176309</td>\n",
       "      <td>2.363721e-19</td>\n",
       "      <td>True</td>\n",
       "      <td>5.515349e-17</td>\n",
       "      <td>True</td>\n",
       "      <td>1.654605e-16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17157</th>\n",
       "      <td>DUP</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>40.0</td>\n",
       "      <td>&gt; 40</td>\n",
       "      <td>upstream_0</td>\n",
       "      <td>gene body</td>\n",
       "      <td>10</td>\n",
       "      <td>803</td>\n",
       "      <td>267</td>\n",
       "      <td>...</td>\n",
       "      <td>52.378475</td>\n",
       "      <td>183.732710</td>\n",
       "      <td>99.324615</td>\n",
       "      <td>52.629240</td>\n",
       "      <td>187.450534</td>\n",
       "      <td>3.515968e-17</td>\n",
       "      <td>True</td>\n",
       "      <td>2.461178e-15</td>\n",
       "      <td>True</td>\n",
       "      <td>2.461178e-14</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>10 rows × 22 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      vrnt_type consequence maf_range  z_cutoff z_cutoff_name     window_raw  \\\n",
       "8453        DUP         all       0-1       2.0           > 2  gene_body_200   \n",
       "9097        DUP         all       0-1      10.0          > 10  gene_body_200   \n",
       "9419        DUP         all       0-1      20.0          > 20  gene_body_200   \n",
       "9741        DUP         all       0-1      30.0          > 30  gene_body_200   \n",
       "16882       DUP         all       0-1       2.0           > 2     upstream_0   \n",
       "10063       DUP         all       0-1      40.0          > 40  gene_body_200   \n",
       "16772       DUP         all       0-1      10.0          > 10     upstream_0   \n",
       "16937       DUP         all       0-1      20.0          > 20     upstream_0   \n",
       "17047       DUP         all       0-1      30.0          > 30     upstream_0   \n",
       "17157       DUP         all       0-1      40.0          > 40     upstream_0   \n",
       "\n",
       "              window_name  misexp_carrier  misexp_total  control_carrier  ...  \\\n",
       "8453   gene body +/-200kb              36         17380             7906  ...   \n",
       "9097   gene body +/-200kb              26         10668             7901  ...   \n",
       "9419   gene body +/-200kb              17          4622             6242  ...   \n",
       "9741   gene body +/-200kb              15          2019             3807  ...   \n",
       "16882           gene body              22         17380              964  ...   \n",
       "10063  gene body +/-200kb              12           803             1680  ...   \n",
       "16772           gene body              19         10668              959  ...   \n",
       "16937           gene body              13          4622              772  ...   \n",
       "17047           gene body              13          2019              496  ...   \n",
       "17157           gene body              10           803              267  ...   \n",
       "\n",
       "       risk_ratio_lower  risk_ratio_upper  odds_ratio  odds_ratio_lower  \\\n",
       "8453           1.930256          3.712788    2.680538          1.931458   \n",
       "9097           2.143761          4.625803    3.154317          2.145324   \n",
       "9419           2.991064          7.736091    4.824383          2.994565   \n",
       "9741           5.611961         15.413631    9.362705          5.628203   \n",
       "16882          8.794992         20.468198   13.432803          8.800693   \n",
       "10063         10.648617         32.870975   18.977759         10.710190   \n",
       "16772         12.044643         29.844000   18.991483         12.055418   \n",
       "16937         17.204574         51.416810   29.823368         17.225280   \n",
       "17047         35.732239        107.118858   62.262029         35.835576   \n",
       "17157         52.378475        183.732710   99.324615         52.629240   \n",
       "\n",
       "       odds_ratio_upper          pval  fdr_bh_pass  fdr_bh_pval_adj  \\\n",
       "8453           3.720134  2.641502e-07         True     3.555869e-06   \n",
       "9097           4.637861  6.369462e-07         True     8.106589e-06   \n",
       "9419           7.772304  2.124559e-07         True     2.974382e-06   \n",
       "9741          15.575174  2.165972e-10         True     5.831464e-09   \n",
       "16882         20.502952  1.220390e-17         True     9.491925e-16   \n",
       "10063         33.627351  5.427455e-12         True     1.651834e-10   \n",
       "16772         29.918202  3.894176e-18         True     4.543206e-16   \n",
       "16937         51.635345  2.500025e-15         True     1.166678e-13   \n",
       "17047        108.176309  2.363721e-19         True     5.515349e-17   \n",
       "17157        187.450534  3.515968e-17         True     2.461178e-15   \n",
       "\n",
       "       bonferroni_pass  bonferroni_pval_adj  \n",
       "8453              True         1.849052e-04  \n",
       "9097              True         4.458624e-04  \n",
       "9419              True         1.487191e-04  \n",
       "9741              True         1.516181e-07  \n",
       "16882             True         8.542733e-15  \n",
       "10063             True         3.799219e-09  \n",
       "16772             True         2.725923e-15  \n",
       "16937             True         1.750018e-12  \n",
       "17047             True         1.654605e-16  \n",
       "17157             True         2.461178e-14  \n",
       "\n",
       "[10 rows x 22 columns]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Duplications windows and gene body enrichment \n",
    "all_enrich_tests_df[all_enrich_tests_df.vrnt_type.isin([\"DUP\"]) & \n",
    "                    (all_enrich_tests_df.consequence == \"all\") &\n",
    "                    (all_enrich_tests_df.bonferroni_pass)\n",
    "                   ].sort_values(by=\"risk_ratio\")\n",
    "# Duplications significantly enriched in gene body + 200kb window all z-scores \n",
    "# Duplications significantly enriched in gene body window all z-scores "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "34b2c5f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>vrnt_type</th>\n",
       "      <th>consequence</th>\n",
       "      <th>maf_range</th>\n",
       "      <th>z_cutoff</th>\n",
       "      <th>z_cutoff_name</th>\n",
       "      <th>window_raw</th>\n",
       "      <th>window_name</th>\n",
       "      <th>misexp_carrier</th>\n",
       "      <th>misexp_total</th>\n",
       "      <th>control_carrier</th>\n",
       "      <th>...</th>\n",
       "      <th>risk_ratio_lower</th>\n",
       "      <th>risk_ratio_upper</th>\n",
       "      <th>odds_ratio</th>\n",
       "      <th>odds_ratio_lower</th>\n",
       "      <th>odds_ratio_upper</th>\n",
       "      <th>pval</th>\n",
       "      <th>fdr_bh_pass</th>\n",
       "      <th>fdr_bh_pval_adj</th>\n",
       "      <th>bonferroni_pass</th>\n",
       "      <th>bonferroni_pval_adj</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>16883</th>\n",
       "      <td>INV</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>&gt; 2</td>\n",
       "      <td>upstream_0</td>\n",
       "      <td>gene body</td>\n",
       "      <td>7</td>\n",
       "      <td>17380</td>\n",
       "      <td>88</td>\n",
       "      <td>...</td>\n",
       "      <td>21.662808</td>\n",
       "      <td>100.957767</td>\n",
       "      <td>46.784119</td>\n",
       "      <td>21.665116</td>\n",
       "      <td>101.026638</td>\n",
       "      <td>3.942279e-10</td>\n",
       "      <td>True</td>\n",
       "      <td>9.515845e-09</td>\n",
       "      <td>True</td>\n",
       "      <td>2.759595e-07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16773</th>\n",
       "      <td>INV</td>\n",
       "      <td>all</td>\n",
       "      <td>0-1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&gt; 10</td>\n",
       "      <td>upstream_0</td>\n",
       "      <td>gene body</td>\n",
       "      <td>7</td>\n",
       "      <td>10668</td>\n",
       "      <td>88</td>\n",
       "      <td>...</td>\n",
       "      <td>35.264085</td>\n",
       "      <td>164.315721</td>\n",
       "      <td>76.170566</td>\n",
       "      <td>35.270407</td>\n",
       "      <td>164.499240</td>\n",
       "      <td>1.374748e-11</td>\n",
       "      <td>True</td>\n",
       "      <td>4.009683e-10</td>\n",
       "      <td>True</td>\n",
       "      <td>9.623239e-09</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 22 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      vrnt_type consequence maf_range  z_cutoff z_cutoff_name  window_raw  \\\n",
       "16883       INV         all       0-1       2.0           > 2  upstream_0   \n",
       "16773       INV         all       0-1      10.0          > 10  upstream_0   \n",
       "\n",
       "      window_name  misexp_carrier  misexp_total  control_carrier  ...  \\\n",
       "16883   gene body               7         17380               88  ...   \n",
       "16773   gene body               7         10668               88  ...   \n",
       "\n",
       "       risk_ratio_lower  risk_ratio_upper  odds_ratio  odds_ratio_lower  \\\n",
       "16883         21.662808        100.957767   46.784119         21.665116   \n",
       "16773         35.264085        164.315721   76.170566         35.270407   \n",
       "\n",
       "       odds_ratio_upper          pval  fdr_bh_pass  fdr_bh_pval_adj  \\\n",
       "16883        101.026638  3.942279e-10         True     9.515845e-09   \n",
       "16773        164.499240  1.374748e-11         True     4.009683e-10   \n",
       "\n",
       "       bonferroni_pass  bonferroni_pval_adj  \n",
       "16883             True         2.759595e-07  \n",
       "16773             True         9.623239e-09  \n",
       "\n",
       "[2 rows x 22 columns]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all_enrich_tests_df[all_enrich_tests_df.vrnt_type.isin([\"INV\"]) & \n",
    "                    (all_enrich_tests_df.consequence == \"all\") &\n",
    "                    (all_enrich_tests_df.bonferroni_pass)\n",
    "                   ].sort_values(by=\"risk_ratio\")\n",
    "# INVs only enriched over gene body for >2 and >10 z-cutoffs "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "138314ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>vrnt_type</th>\n",
       "      <th>consequence</th>\n",
       "      <th>maf_range</th>\n",
       "      <th>z_cutoff</th>\n",
       "      <th>z_cutoff_name</th>\n",
       "      <th>window_raw</th>\n",
       "      <th>window_name</th>\n",
       "      <th>misexp_carrier</th>\n",
       "      <th>misexp_total</th>\n",
       "      <th>control_carrier</th>\n",
       "      <th>...</th>\n",
       "      <th>risk_ratio_lower</th>\n",
       "      <th>risk_ratio_upper</th>\n",
       "      <th>odds_ratio</th>\n",
       "      <th>odds_ratio_lower</th>\n",
       "      <th>odds_ratio_upper</th>\n",
       "      <th>pval</th>\n",
       "      <th>fdr_bh_pass</th>\n",
       "      <th>fdr_bh_pval_adj</th>\n",
       "      <th>bonferroni_pass</th>\n",
       "      <th>bonferroni_pval_adj</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>0 rows × 22 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [vrnt_type, consequence, maf_range, z_cutoff, z_cutoff_name, window_raw, window_name, misexp_carrier, misexp_total, control_carrier, control_total, risk_ratio, risk_ratio_lower, risk_ratio_upper, odds_ratio, odds_ratio_lower, odds_ratio_upper, pval, fdr_bh_pass, fdr_bh_pval_adj, bonferroni_pass, bonferroni_pval_adj]\n",
       "Index: []\n",
       "\n",
       "[0 rows x 22 columns]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# MEI enrichment \n",
    "all_enrich_tests_df[all_enrich_tests_df.vrnt_type.isin([\"MEI\"]) & \n",
    "                    (all_enrich_tests_df.consequence == \"all\") &\n",
    "                    (all_enrich_tests_df.bonferroni_pass)\n",
    "                   ].sort_values(by=\"risk_ratio\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "678af7dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>vrnt_type</th>\n",
       "      <th>consequence</th>\n",
       "      <th>maf_range</th>\n",
       "      <th>z_cutoff</th>\n",
       "      <th>z_cutoff_name</th>\n",
       "      <th>window_raw</th>\n",
       "      <th>window_name</th>\n",
       "      <th>misexp_carrier</th>\n",
       "      <th>misexp_total</th>\n",
       "      <th>control_carrier</th>\n",
       "      <th>...</th>\n",
       "      <th>risk_ratio_lower</th>\n",
       "      <th>risk_ratio_upper</th>\n",
       "      <th>odds_ratio</th>\n",
       "      <th>odds_ratio_lower</th>\n",
       "      <th>odds_ratio_upper</th>\n",
       "      <th>pval</th>\n",
       "      <th>fdr_bh_pass</th>\n",
       "      <th>fdr_bh_pval_adj</th>\n",
       "      <th>bonferroni_pass</th>\n",
       "      <th>bonferroni_pval_adj</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>80</th>\n",
       "      <td>DEL</td>\n",
       "      <td>no_predicted_effect</td>\n",
       "      <td>0-1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>&gt; 2</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>139</td>\n",
       "      <td>17380</td>\n",
       "      <td>53793</td>\n",
       "      <td>...</td>\n",
       "      <td>1.287061</td>\n",
       "      <td>1.793093</td>\n",
       "      <td>1.523336</td>\n",
       "      <td>1.288885</td>\n",
       "      <td>1.800436</td>\n",
       "      <td>3.596051e-06</td>\n",
       "      <td>True</td>\n",
       "      <td>3.701817e-05</td>\n",
       "      <td>True</td>\n",
       "      <td>0.002517</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>81</th>\n",
       "      <td>DEL</td>\n",
       "      <td>no_predicted_effect</td>\n",
       "      <td>0-1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&gt; 10</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>97</td>\n",
       "      <td>10668</td>\n",
       "      <td>53619</td>\n",
       "      <td>...</td>\n",
       "      <td>1.419819</td>\n",
       "      <td>2.110835</td>\n",
       "      <td>1.737895</td>\n",
       "      <td>1.422732</td>\n",
       "      <td>2.122871</td>\n",
       "      <td>5.889020e-07</td>\n",
       "      <td>True</td>\n",
       "      <td>7.633915e-06</td>\n",
       "      <td>True</td>\n",
       "      <td>0.000412</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82</th>\n",
       "      <td>DEL</td>\n",
       "      <td>no_predicted_effect</td>\n",
       "      <td>0-1</td>\n",
       "      <td>20.0</td>\n",
       "      <td>&gt; 20</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>50</td>\n",
       "      <td>4622</td>\n",
       "      <td>41569</td>\n",
       "      <td>...</td>\n",
       "      <td>1.612320</td>\n",
       "      <td>2.799281</td>\n",
       "      <td>2.136759</td>\n",
       "      <td>1.616772</td>\n",
       "      <td>2.823985</td>\n",
       "      <td>1.445409e-06</td>\n",
       "      <td>True</td>\n",
       "      <td>1.556594e-05</td>\n",
       "      <td>True</td>\n",
       "      <td>0.001012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>83</th>\n",
       "      <td>DEL</td>\n",
       "      <td>no_predicted_effect</td>\n",
       "      <td>0-1</td>\n",
       "      <td>30.0</td>\n",
       "      <td>&gt; 30</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>25</td>\n",
       "      <td>2019</td>\n",
       "      <td>23555</td>\n",
       "      <td>...</td>\n",
       "      <td>1.696621</td>\n",
       "      <td>3.699404</td>\n",
       "      <td>2.524165</td>\n",
       "      <td>1.701074</td>\n",
       "      <td>3.745519</td>\n",
       "      <td>4.324340e-05</td>\n",
       "      <td>True</td>\n",
       "      <td>3.563139e-04</td>\n",
       "      <td>True</td>\n",
       "      <td>0.030270</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>45</th>\n",
       "      <td>DEL</td>\n",
       "      <td>upstream_gene_variant</td>\n",
       "      <td>0-1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>&gt; 2</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>12</td>\n",
       "      <td>17380</td>\n",
       "      <td>1524</td>\n",
       "      <td>...</td>\n",
       "      <td>2.623654</td>\n",
       "      <td>8.167887</td>\n",
       "      <td>4.631731</td>\n",
       "      <td>2.624052</td>\n",
       "      <td>8.175499</td>\n",
       "      <td>1.844943e-05</td>\n",
       "      <td>True</td>\n",
       "      <td>1.655718e-04</td>\n",
       "      <td>True</td>\n",
       "      <td>0.012915</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>84</th>\n",
       "      <td>DEL</td>\n",
       "      <td>no_predicted_effect</td>\n",
       "      <td>0-1</td>\n",
       "      <td>40.0</td>\n",
       "      <td>&gt; 40</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>19</td>\n",
       "      <td>803</td>\n",
       "      <td>9741</td>\n",
       "      <td>...</td>\n",
       "      <td>3.274796</td>\n",
       "      <td>7.970355</td>\n",
       "      <td>5.208521</td>\n",
       "      <td>3.302892</td>\n",
       "      <td>8.213618</td>\n",
       "      <td>1.483367e-08</td>\n",
       "      <td>True</td>\n",
       "      <td>2.532578e-07</td>\n",
       "      <td>True</td>\n",
       "      <td>0.000010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>46</th>\n",
       "      <td>DEL</td>\n",
       "      <td>upstream_gene_variant</td>\n",
       "      <td>0-1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&gt; 10</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>10</td>\n",
       "      <td>10668</td>\n",
       "      <td>1526</td>\n",
       "      <td>...</td>\n",
       "      <td>3.368277</td>\n",
       "      <td>11.675195</td>\n",
       "      <td>6.275933</td>\n",
       "      <td>3.368979</td>\n",
       "      <td>11.691181</td>\n",
       "      <td>7.104095e-06</td>\n",
       "      <td>True</td>\n",
       "      <td>6.812146e-05</td>\n",
       "      <td>True</td>\n",
       "      <td>0.004973</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>DEL</td>\n",
       "      <td>non_coding_transcript_exon_variant</td>\n",
       "      <td>0-1</td>\n",
       "      <td>40.0</td>\n",
       "      <td>&gt; 40</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>4</td>\n",
       "      <td>803</td>\n",
       "      <td>329</td>\n",
       "      <td>...</td>\n",
       "      <td>11.910231</td>\n",
       "      <td>85.147102</td>\n",
       "      <td>31.999696</td>\n",
       "      <td>11.909887</td>\n",
       "      <td>85.977350</td>\n",
       "      <td>9.588540e-06</td>\n",
       "      <td>True</td>\n",
       "      <td>8.831550e-05</td>\n",
       "      <td>True</td>\n",
       "      <td>0.006712</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8 rows × 22 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   vrnt_type                         consequence maf_range  z_cutoff  \\\n",
       "80       DEL                 no_predicted_effect       0-1       2.0   \n",
       "81       DEL                 no_predicted_effect       0-1      10.0   \n",
       "82       DEL                 no_predicted_effect       0-1      20.0   \n",
       "83       DEL                 no_predicted_effect       0-1      30.0   \n",
       "45       DEL               upstream_gene_variant       0-1       2.0   \n",
       "84       DEL                 no_predicted_effect       0-1      40.0   \n",
       "46       DEL               upstream_gene_variant       0-1      10.0   \n",
       "39       DEL  non_coding_transcript_exon_variant       0-1      40.0   \n",
       "\n",
       "   z_cutoff_name     window_raw         window_name  misexp_carrier  \\\n",
       "80           > 2  gene_body_200  gene body +/-200kb             139   \n",
       "81          > 10  gene_body_200  gene body +/-200kb              97   \n",
       "82          > 20  gene_body_200  gene body +/-200kb              50   \n",
       "83          > 30  gene_body_200  gene body +/-200kb              25   \n",
       "45           > 2  gene_body_200  gene body +/-200kb              12   \n",
       "84          > 40  gene_body_200  gene body +/-200kb              19   \n",
       "46          > 10  gene_body_200  gene body +/-200kb              10   \n",
       "39          > 40  gene_body_200  gene body +/-200kb               4   \n",
       "\n",
       "    misexp_total  control_carrier  ...  risk_ratio_lower  risk_ratio_upper  \\\n",
       "80         17380            53793  ...          1.287061          1.793093   \n",
       "81         10668            53619  ...          1.419819          2.110835   \n",
       "82          4622            41569  ...          1.612320          2.799281   \n",
       "83          2019            23555  ...          1.696621          3.699404   \n",
       "45         17380             1524  ...          2.623654          8.167887   \n",
       "84           803             9741  ...          3.274796          7.970355   \n",
       "46         10668             1526  ...          3.368277         11.675195   \n",
       "39           803              329  ...         11.910231         85.147102   \n",
       "\n",
       "    odds_ratio  odds_ratio_lower  odds_ratio_upper          pval  fdr_bh_pass  \\\n",
       "80    1.523336          1.288885          1.800436  3.596051e-06         True   \n",
       "81    1.737895          1.422732          2.122871  5.889020e-07         True   \n",
       "82    2.136759          1.616772          2.823985  1.445409e-06         True   \n",
       "83    2.524165          1.701074          3.745519  4.324340e-05         True   \n",
       "45    4.631731          2.624052          8.175499  1.844943e-05         True   \n",
       "84    5.208521          3.302892          8.213618  1.483367e-08         True   \n",
       "46    6.275933          3.368979         11.691181  7.104095e-06         True   \n",
       "39   31.999696         11.909887         85.977350  9.588540e-06         True   \n",
       "\n",
       "    fdr_bh_pval_adj  bonferroni_pass  bonferroni_pval_adj  \n",
       "80     3.701817e-05             True             0.002517  \n",
       "81     7.633915e-06             True             0.000412  \n",
       "82     1.556594e-05             True             0.001012  \n",
       "83     3.563139e-04             True             0.030270  \n",
       "45     1.655718e-04             True             0.012915  \n",
       "84     2.532578e-07             True             0.000010  \n",
       "46     6.812146e-05             True             0.004973  \n",
       "39     8.831550e-05             True             0.006712  \n",
       "\n",
       "[8 rows x 22 columns]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Deletion consequences enriched\n",
    "all_enrich_tests_df[all_enrich_tests_df.vrnt_type.isin([\"DEL\"]) & \n",
    "                    (all_enrich_tests_df.consequence != \"all\") &\n",
    "                    (all_enrich_tests_df.window_raw == \"gene_body_200\") &\n",
    "                    (all_enrich_tests_df.bonferroni_pass)\n",
    "                   ].sort_values(by=\"risk_ratio\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "9efe8d3e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>vrnt_type</th>\n",
       "      <th>consequence</th>\n",
       "      <th>maf_range</th>\n",
       "      <th>z_cutoff</th>\n",
       "      <th>z_cutoff_name</th>\n",
       "      <th>window_raw</th>\n",
       "      <th>window_name</th>\n",
       "      <th>misexp_carrier</th>\n",
       "      <th>misexp_total</th>\n",
       "      <th>control_carrier</th>\n",
       "      <th>...</th>\n",
       "      <th>risk_ratio_lower</th>\n",
       "      <th>risk_ratio_upper</th>\n",
       "      <th>odds_ratio</th>\n",
       "      <th>odds_ratio_lower</th>\n",
       "      <th>odds_ratio_upper</th>\n",
       "      <th>pval</th>\n",
       "      <th>fdr_bh_pass</th>\n",
       "      <th>fdr_bh_pval_adj</th>\n",
       "      <th>bonferroni_pass</th>\n",
       "      <th>bonferroni_pval_adj</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>140</th>\n",
       "      <td>DUP</td>\n",
       "      <td>non_coding_transcript_exon_variant</td>\n",
       "      <td>0-1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>&gt; 2</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>8</td>\n",
       "      <td>17380</td>\n",
       "      <td>349</td>\n",
       "      <td>...</td>\n",
       "      <td>6.687598</td>\n",
       "      <td>27.157057</td>\n",
       "      <td>13.482224</td>\n",
       "      <td>6.688336</td>\n",
       "      <td>27.177221</td>\n",
       "      <td>2.468041e-07</td>\n",
       "      <td>True</td>\n",
       "      <td>3.387507e-06</td>\n",
       "      <td>True</td>\n",
       "      <td>0.000173</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>120</th>\n",
       "      <td>DUP</td>\n",
       "      <td>transcript_amplification</td>\n",
       "      <td>0-1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>&gt; 2</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>9</td>\n",
       "      <td>17380</td>\n",
       "      <td>284</td>\n",
       "      <td>...</td>\n",
       "      <td>9.596524</td>\n",
       "      <td>36.170803</td>\n",
       "      <td>18.640130</td>\n",
       "      <td>9.598025</td>\n",
       "      <td>36.200617</td>\n",
       "      <td>2.942343e-09</td>\n",
       "      <td>True</td>\n",
       "      <td>6.241334e-08</td>\n",
       "      <td>True</td>\n",
       "      <td>0.000002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>121</th>\n",
       "      <td>DUP</td>\n",
       "      <td>transcript_amplification</td>\n",
       "      <td>0-1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&gt; 10</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>6</td>\n",
       "      <td>10668</td>\n",
       "      <td>287</td>\n",
       "      <td>...</td>\n",
       "      <td>8.915413</td>\n",
       "      <td>44.892958</td>\n",
       "      <td>20.016677</td>\n",
       "      <td>8.916202</td>\n",
       "      <td>44.936996</td>\n",
       "      <td>8.348578e-07</td>\n",
       "      <td>True</td>\n",
       "      <td>9.905092e-06</td>\n",
       "      <td>True</td>\n",
       "      <td>0.000584</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>141</th>\n",
       "      <td>DUP</td>\n",
       "      <td>non_coding_transcript_exon_variant</td>\n",
       "      <td>0-1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&gt; 10</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>8</td>\n",
       "      <td>10668</td>\n",
       "      <td>349</td>\n",
       "      <td>...</td>\n",
       "      <td>10.886590</td>\n",
       "      <td>44.199589</td>\n",
       "      <td>21.951592</td>\n",
       "      <td>10.888784</td>\n",
       "      <td>44.254011</td>\n",
       "      <td>6.156241e-09</td>\n",
       "      <td>True</td>\n",
       "      <td>1.165118e-07</td>\n",
       "      <td>True</td>\n",
       "      <td>0.000004</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>142</th>\n",
       "      <td>DUP</td>\n",
       "      <td>non_coding_transcript_exon_variant</td>\n",
       "      <td>0-1</td>\n",
       "      <td>20.0</td>\n",
       "      <td>&gt; 20</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>5</td>\n",
       "      <td>4622</td>\n",
       "      <td>314</td>\n",
       "      <td>...</td>\n",
       "      <td>11.630692</td>\n",
       "      <td>68.009897</td>\n",
       "      <td>28.154135</td>\n",
       "      <td>11.631880</td>\n",
       "      <td>68.145078</td>\n",
       "      <td>1.332198e-06</td>\n",
       "      <td>True</td>\n",
       "      <td>1.457091e-05</td>\n",
       "      <td>True</td>\n",
       "      <td>0.000933</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>122</th>\n",
       "      <td>DUP</td>\n",
       "      <td>transcript_amplification</td>\n",
       "      <td>0-1</td>\n",
       "      <td>20.0</td>\n",
       "      <td>&gt; 20</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>4</td>\n",
       "      <td>4622</td>\n",
       "      <td>209</td>\n",
       "      <td>...</td>\n",
       "      <td>12.574495</td>\n",
       "      <td>90.872781</td>\n",
       "      <td>33.831954</td>\n",
       "      <td>12.574486</td>\n",
       "      <td>91.025679</td>\n",
       "      <td>7.767531e-06</td>\n",
       "      <td>True</td>\n",
       "      <td>7.347665e-05</td>\n",
       "      <td>True</td>\n",
       "      <td>0.005437</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>127</th>\n",
       "      <td>DUP</td>\n",
       "      <td>coding_sequence_variant</td>\n",
       "      <td>0-1</td>\n",
       "      <td>20.0</td>\n",
       "      <td>&gt; 20</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>3</td>\n",
       "      <td>4622</td>\n",
       "      <td>104</td>\n",
       "      <td>...</td>\n",
       "      <td>16.173894</td>\n",
       "      <td>160.493773</td>\n",
       "      <td>50.981527</td>\n",
       "      <td>16.172468</td>\n",
       "      <td>160.712401</td>\n",
       "      <td>3.438990e-05</td>\n",
       "      <td>True</td>\n",
       "      <td>2.971967e-04</td>\n",
       "      <td>True</td>\n",
       "      <td>0.024073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143</th>\n",
       "      <td>DUP</td>\n",
       "      <td>non_coding_transcript_exon_variant</td>\n",
       "      <td>0-1</td>\n",
       "      <td>30.0</td>\n",
       "      <td>&gt; 30</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>5</td>\n",
       "      <td>2019</td>\n",
       "      <td>230</td>\n",
       "      <td>...</td>\n",
       "      <td>21.180003</td>\n",
       "      <td>124.325765</td>\n",
       "      <td>51.439823</td>\n",
       "      <td>21.185939</td>\n",
       "      <td>124.896773</td>\n",
       "      <td>7.150757e-08</td>\n",
       "      <td>True</td>\n",
       "      <td>1.112340e-06</td>\n",
       "      <td>True</td>\n",
       "      <td>0.000050</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>123</th>\n",
       "      <td>DUP</td>\n",
       "      <td>transcript_amplification</td>\n",
       "      <td>0-1</td>\n",
       "      <td>30.0</td>\n",
       "      <td>&gt; 30</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>4</td>\n",
       "      <td>2019</td>\n",
       "      <td>124</td>\n",
       "      <td>...</td>\n",
       "      <td>28.160751</td>\n",
       "      <td>205.889974</td>\n",
       "      <td>76.293877</td>\n",
       "      <td>28.161975</td>\n",
       "      <td>206.688472</td>\n",
       "      <td>3.279864e-07</td>\n",
       "      <td>True</td>\n",
       "      <td>4.331896e-06</td>\n",
       "      <td>True</td>\n",
       "      <td>0.000230</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>128</th>\n",
       "      <td>DUP</td>\n",
       "      <td>coding_sequence_variant</td>\n",
       "      <td>0-1</td>\n",
       "      <td>30.0</td>\n",
       "      <td>&gt; 30</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>3</td>\n",
       "      <td>2019</td>\n",
       "      <td>76</td>\n",
       "      <td>...</td>\n",
       "      <td>29.418491</td>\n",
       "      <td>295.119390</td>\n",
       "      <td>93.314243</td>\n",
       "      <td>29.413195</td>\n",
       "      <td>296.042233</td>\n",
       "      <td>5.853279e-06</td>\n",
       "      <td>True</td>\n",
       "      <td>5.690688e-05</td>\n",
       "      <td>True</td>\n",
       "      <td>0.004097</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>124</th>\n",
       "      <td>DUP</td>\n",
       "      <td>transcript_amplification</td>\n",
       "      <td>0-1</td>\n",
       "      <td>40.0</td>\n",
       "      <td>&gt; 40</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>3</td>\n",
       "      <td>803</td>\n",
       "      <td>83</td>\n",
       "      <td>...</td>\n",
       "      <td>29.983548</td>\n",
       "      <td>298.927005</td>\n",
       "      <td>95.023825</td>\n",
       "      <td>29.969788</td>\n",
       "      <td>301.287664</td>\n",
       "      <td>5.534891e-06</td>\n",
       "      <td>True</td>\n",
       "      <td>5.456935e-05</td>\n",
       "      <td>True</td>\n",
       "      <td>0.003874</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>144</th>\n",
       "      <td>DUP</td>\n",
       "      <td>non_coding_transcript_exon_variant</td>\n",
       "      <td>0-1</td>\n",
       "      <td>40.0</td>\n",
       "      <td>&gt; 40</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>4</td>\n",
       "      <td>803</td>\n",
       "      <td>97</td>\n",
       "      <td>...</td>\n",
       "      <td>39.831096</td>\n",
       "      <td>292.897803</td>\n",
       "      <td>108.547024</td>\n",
       "      <td>39.837067</td>\n",
       "      <td>295.766163</td>\n",
       "      <td>8.347261e-08</td>\n",
       "      <td>True</td>\n",
       "      <td>1.270235e-06</td>\n",
       "      <td>True</td>\n",
       "      <td>0.000058</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>12 rows × 22 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    vrnt_type                         consequence maf_range  z_cutoff  \\\n",
       "140       DUP  non_coding_transcript_exon_variant       0-1       2.0   \n",
       "120       DUP            transcript_amplification       0-1       2.0   \n",
       "121       DUP            transcript_amplification       0-1      10.0   \n",
       "141       DUP  non_coding_transcript_exon_variant       0-1      10.0   \n",
       "142       DUP  non_coding_transcript_exon_variant       0-1      20.0   \n",
       "122       DUP            transcript_amplification       0-1      20.0   \n",
       "127       DUP             coding_sequence_variant       0-1      20.0   \n",
       "143       DUP  non_coding_transcript_exon_variant       0-1      30.0   \n",
       "123       DUP            transcript_amplification       0-1      30.0   \n",
       "128       DUP             coding_sequence_variant       0-1      30.0   \n",
       "124       DUP            transcript_amplification       0-1      40.0   \n",
       "144       DUP  non_coding_transcript_exon_variant       0-1      40.0   \n",
       "\n",
       "    z_cutoff_name     window_raw         window_name  misexp_carrier  \\\n",
       "140           > 2  gene_body_200  gene body +/-200kb               8   \n",
       "120           > 2  gene_body_200  gene body +/-200kb               9   \n",
       "121          > 10  gene_body_200  gene body +/-200kb               6   \n",
       "141          > 10  gene_body_200  gene body +/-200kb               8   \n",
       "142          > 20  gene_body_200  gene body +/-200kb               5   \n",
       "122          > 20  gene_body_200  gene body +/-200kb               4   \n",
       "127          > 20  gene_body_200  gene body +/-200kb               3   \n",
       "143          > 30  gene_body_200  gene body +/-200kb               5   \n",
       "123          > 30  gene_body_200  gene body +/-200kb               4   \n",
       "128          > 30  gene_body_200  gene body +/-200kb               3   \n",
       "124          > 40  gene_body_200  gene body +/-200kb               3   \n",
       "144          > 40  gene_body_200  gene body +/-200kb               4   \n",
       "\n",
       "     misexp_total  control_carrier  ...  risk_ratio_lower  risk_ratio_upper  \\\n",
       "140         17380              349  ...          6.687598         27.157057   \n",
       "120         17380              284  ...          9.596524         36.170803   \n",
       "121         10668              287  ...          8.915413         44.892958   \n",
       "141         10668              349  ...         10.886590         44.199589   \n",
       "142          4622              314  ...         11.630692         68.009897   \n",
       "122          4622              209  ...         12.574495         90.872781   \n",
       "127          4622              104  ...         16.173894        160.493773   \n",
       "143          2019              230  ...         21.180003        124.325765   \n",
       "123          2019              124  ...         28.160751        205.889974   \n",
       "128          2019               76  ...         29.418491        295.119390   \n",
       "124           803               83  ...         29.983548        298.927005   \n",
       "144           803               97  ...         39.831096        292.897803   \n",
       "\n",
       "     odds_ratio  odds_ratio_lower  odds_ratio_upper          pval  \\\n",
       "140   13.482224          6.688336         27.177221  2.468041e-07   \n",
       "120   18.640130          9.598025         36.200617  2.942343e-09   \n",
       "121   20.016677          8.916202         44.936996  8.348578e-07   \n",
       "141   21.951592         10.888784         44.254011  6.156241e-09   \n",
       "142   28.154135         11.631880         68.145078  1.332198e-06   \n",
       "122   33.831954         12.574486         91.025679  7.767531e-06   \n",
       "127   50.981527         16.172468        160.712401  3.438990e-05   \n",
       "143   51.439823         21.185939        124.896773  7.150757e-08   \n",
       "123   76.293877         28.161975        206.688472  3.279864e-07   \n",
       "128   93.314243         29.413195        296.042233  5.853279e-06   \n",
       "124   95.023825         29.969788        301.287664  5.534891e-06   \n",
       "144  108.547024         39.837067        295.766163  8.347261e-08   \n",
       "\n",
       "     fdr_bh_pass  fdr_bh_pval_adj  bonferroni_pass  bonferroni_pval_adj  \n",
       "140         True     3.387507e-06             True             0.000173  \n",
       "120         True     6.241334e-08             True             0.000002  \n",
       "121         True     9.905092e-06             True             0.000584  \n",
       "141         True     1.165118e-07             True             0.000004  \n",
       "142         True     1.457091e-05             True             0.000933  \n",
       "122         True     7.347665e-05             True             0.005437  \n",
       "127         True     2.971967e-04             True             0.024073  \n",
       "143         True     1.112340e-06             True             0.000050  \n",
       "123         True     4.331896e-06             True             0.000230  \n",
       "128         True     5.690688e-05             True             0.004097  \n",
       "124         True     5.456935e-05             True             0.003874  \n",
       "144         True     1.270235e-06             True             0.000058  \n",
       "\n",
       "[12 rows x 22 columns]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Duplication consequences \n",
    "all_enrich_tests_df[all_enrich_tests_df.vrnt_type.isin([\"DUP\"]) & \n",
    "                    (all_enrich_tests_df.consequence != \"all\") &\n",
    "                    (all_enrich_tests_df.window_raw == \"gene_body_200\") &\n",
    "                    (all_enrich_tests_df.bonferroni_pass)\n",
    "                   ].sort_values(by=\"risk_ratio\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "2358e235",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>vrnt_type</th>\n",
       "      <th>consequence</th>\n",
       "      <th>maf_range</th>\n",
       "      <th>z_cutoff</th>\n",
       "      <th>z_cutoff_name</th>\n",
       "      <th>window_raw</th>\n",
       "      <th>window_name</th>\n",
       "      <th>misexp_carrier</th>\n",
       "      <th>misexp_total</th>\n",
       "      <th>control_carrier</th>\n",
       "      <th>...</th>\n",
       "      <th>risk_ratio_lower</th>\n",
       "      <th>risk_ratio_upper</th>\n",
       "      <th>odds_ratio</th>\n",
       "      <th>odds_ratio_lower</th>\n",
       "      <th>odds_ratio_upper</th>\n",
       "      <th>pval</th>\n",
       "      <th>fdr_bh_pass</th>\n",
       "      <th>fdr_bh_pval_adj</th>\n",
       "      <th>bonferroni_pass</th>\n",
       "      <th>bonferroni_pval_adj</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>192</th>\n",
       "      <td>INV</td>\n",
       "      <td>coding_sequence_variant</td>\n",
       "      <td>0-1</td>\n",
       "      <td>20.0</td>\n",
       "      <td>&gt; 20</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>2</td>\n",
       "      <td>4622</td>\n",
       "      <td>12</td>\n",
       "      <td>...</td>\n",
       "      <td>65.902509</td>\n",
       "      <td>1314.899323</td>\n",
       "      <td>294.499495</td>\n",
       "      <td>65.894318</td>\n",
       "      <td>1316.197749</td>\n",
       "      <td>2.899973e-05</td>\n",
       "      <td>True</td>\n",
       "      <td>2.537476e-04</td>\n",
       "      <td>True</td>\n",
       "      <td>2.029981e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>190</th>\n",
       "      <td>INV</td>\n",
       "      <td>coding_sequence_variant</td>\n",
       "      <td>0-1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>&gt; 2</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>7</td>\n",
       "      <td>17380</td>\n",
       "      <td>7</td>\n",
       "      <td>...</td>\n",
       "      <td>206.239290</td>\n",
       "      <td>1675.916381</td>\n",
       "      <td>588.147873</td>\n",
       "      <td>206.278641</td>\n",
       "      <td>1676.944930</td>\n",
       "      <td>1.380879e-16</td>\n",
       "      <td>True</td>\n",
       "      <td>8.787410e-15</td>\n",
       "      <td>True</td>\n",
       "      <td>9.666151e-14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>191</th>\n",
       "      <td>INV</td>\n",
       "      <td>coding_sequence_variant</td>\n",
       "      <td>0-1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>&gt; 10</td>\n",
       "      <td>gene_body_200</td>\n",
       "      <td>gene body +/-200kb</td>\n",
       "      <td>7</td>\n",
       "      <td>10668</td>\n",
       "      <td>7</td>\n",
       "      <td>...</td>\n",
       "      <td>335.721268</td>\n",
       "      <td>2727.734770</td>\n",
       "      <td>957.580433</td>\n",
       "      <td>335.825865</td>\n",
       "      <td>2730.463555</td>\n",
       "      <td>4.597358e-18</td>\n",
       "      <td>True</td>\n",
       "      <td>4.597358e-16</td>\n",
       "      <td>True</td>\n",
       "      <td>3.218150e-15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3 rows × 22 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    vrnt_type              consequence maf_range  z_cutoff z_cutoff_name  \\\n",
       "192       INV  coding_sequence_variant       0-1      20.0          > 20   \n",
       "190       INV  coding_sequence_variant       0-1       2.0           > 2   \n",
       "191       INV  coding_sequence_variant       0-1      10.0          > 10   \n",
       "\n",
       "        window_raw         window_name  misexp_carrier  misexp_total  \\\n",
       "192  gene_body_200  gene body +/-200kb               2          4622   \n",
       "190  gene_body_200  gene body +/-200kb               7         17380   \n",
       "191  gene_body_200  gene body +/-200kb               7         10668   \n",
       "\n",
       "     control_carrier  ...  risk_ratio_lower  risk_ratio_upper  odds_ratio  \\\n",
       "192               12  ...         65.902509       1314.899323  294.499495   \n",
       "190                7  ...        206.239290       1675.916381  588.147873   \n",
       "191                7  ...        335.721268       2727.734770  957.580433   \n",
       "\n",
       "     odds_ratio_lower  odds_ratio_upper          pval  fdr_bh_pass  \\\n",
       "192         65.894318       1316.197749  2.899973e-05         True   \n",
       "190        206.278641       1676.944930  1.380879e-16         True   \n",
       "191        335.825865       2730.463555  4.597358e-18         True   \n",
       "\n",
       "     fdr_bh_pval_adj  bonferroni_pass  bonferroni_pval_adj  \n",
       "192     2.537476e-04             True         2.029981e-02  \n",
       "190     8.787410e-15             True         9.666151e-14  \n",
       "191     4.597358e-16             True         3.218150e-15  \n",
       "\n",
       "[3 rows x 22 columns]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Inversion consequences \n",
    "all_enrich_tests_df[all_enrich_tests_df.vrnt_type.isin([\"INV\"]) & \n",
    "                    (all_enrich_tests_df.consequence != \"all\") &\n",
    "                    (all_enrich_tests_df.window_raw == \"gene_body_200\") &\n",
    "                    (all_enrich_tests_df.bonferroni_pass)\n",
    "                   ].sort_values(by=\"risk_ratio\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "b5f5289c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# change TES to TTS (transcription termination site)\n",
    "tes_to_tts_replace = {\"TES to 200kb\": \"TTS to 200kb\"}\n",
    "all_enrich_tests_df.window_name = all_enrich_tests_df.window_name.replace(tes_to_tts_replace)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "a1c636c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# check if rare SVs 200kb to TSS are significant across all z-score cutoffs \n",
    "all_enrich_tests_df[(all_enrich_tests_df.vrnt_type == \"all_sv\") & \n",
    "                    (all_enrich_tests_df.window_raw == \"upstream_200000\") &\n",
    "                    (all_enrich_tests_df.maf_range == \"0-1\")\n",
    "                   ].bonferroni_pass.unique()\n",
    "# window remains significant up to 200kb z-score cutoff "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93455490",
   "metadata": {},
   "source": [
    "### Subset enrichments for figures  \n",
    "\n",
    "1. SNV, indel and SV - z-score cutoffs and MAF cutoff \n",
    "2. SNV, indel and SV windows analysis \n",
    "3. SV types - Cutoff, Bonferroni at z-score = 10 \n",
    "    * Only includes DELs and DUPs \n",
    "    * Supplementary figure with other z-score cutoffs \n",
    "4. Windows - DEL, DUP, INV \n",
    "    * Supplementary figure with other z-score cutoffs\n",
    "5. SV consequences "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "69a2e47c",
   "metadata": {},
   "outputs": [],
   "source": [
    "subset_enrich_path = outdir_path.joinpath(\"grouped_enrich_gene_body_10kb\")\n",
    "subset_enrich_path.mkdir(parents=\"True\", exist_ok=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "b76212be",
   "metadata": {},
   "outputs": [],
   "source": [
    "### SNV, Indel and SV comparison \n",
    "# SNV, indel and SV - gene window results, all MAF +/- 10kb windows \n",
    "snp_indel_sv_gene_window_df = all_enrich_tests_df[all_enrich_tests_df.vrnt_type.isin([\"all_sv\", \"indel\", \"snp\"]) &\n",
    "                                                  all_enrich_tests_df.window_raw.isin([\"gene_body_10\", \"gene_body_window_10000\"])]\n",
    "snp_indel_sv_gene_window_df.to_csv(subset_enrich_path.joinpath(\"snp_indel_sv_gene_window_results.tsv\"), sep=\"\\t\", index=False)\n",
    "\n",
    "# SNV, indel and SV - windows, MAF < 1%  \n",
    "snp_indel_sv_window_df = all_enrich_tests_df[all_enrich_tests_df.vrnt_type.isin([\"all_sv\", \"indel\", \"snp\"]) &\n",
    "                                                 (all_enrich_tests_df.maf_range == \"0-1\") &\n",
    "                                                 ~(all_enrich_tests_df.window_raw.isin([\"gene_body_10\", \"gene_body_200\", \"gene_body_window_10000\"]))]\n",
    "snp_indel_sv_window_df.to_csv(subset_enrich_path.joinpath(\"snp_indel_sv_maf_0_1perc_window_bin_results.tsv\"), sep=\"\\t\", index=False)  \n",
    "\n",
    "### SV variant types enrich, MAF < 1%, z cutoffs\n",
    "# select rare (MAF < 1%) SV types, gene body +/- 200kb\n",
    "sv_types_rare_200kb_results_df = all_enrich_tests_df[(all_enrich_tests_df.vrnt_type.isin([\"DEL\", \"DUP\", \"INV\", \"MEI\"])) &\n",
    "                                                    (all_enrich_tests_df.window_raw.isin([\"gene_body_200\"])) & \n",
    "                                                    (all_enrich_tests_df.maf_range.isin([\"0-1\"])) &\n",
    "                                                    (all_enrich_tests_df.consequence == \"all\")  \n",
    "                                                   ]\n",
    "sv_types_rare_200kb_results_df.to_csv(subset_enrich_path.joinpath(\"sv_types_maf_0_1perc_200kb_all_z_results.tsv\"), sep=\"\\t\", index=False)\n",
    "\n",
    "# windows \n",
    "sv_types_rare_windows_results_df = all_enrich_tests_df[(all_enrich_tests_df.vrnt_type.isin([\"DEL\", \"DUP\", \"INV\", \"MEI\"])) &\n",
    "                                                    ~(all_enrich_tests_df.window_raw.isin([\"gene_body_10\", \"gene_body_200\"])) & \n",
    "                                                    (all_enrich_tests_df.maf_range.isin([\"0-1\"])) &\n",
    "                                                    (all_enrich_tests_df.consequence == \"all\")  \n",
    "                                                   ]\n",
    "sv_types_rare_windows_results_df.to_csv(subset_enrich_path.joinpath(\"sv_types_maf_0_1perc_windows_all_z_results.tsv\"), sep=\"\\t\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "bba96d16",
   "metadata": {},
   "outputs": [],
   "source": [
    "# all consequences pass Bonferroni\n",
    "sv_vep_enrich_tests_df = all_enrich_tests_df[all_enrich_tests_df.vrnt_type.isin([\"INV\", \"DUP\", \"DEL\"]) & \n",
    "                                             (all_enrich_tests_df.consequence != \"all\") &\n",
    "                                             (all_enrich_tests_df.window_raw == \"gene_body_200\")\n",
    "                                            ].copy()\n",
    "sv_vep_enrich_tests_df[\"vrnt_type_consequence\"] = sv_vep_enrich_tests_df.vrnt_type + \"|\" + sv_vep_enrich_tests_df.consequence\n",
    "\n",
    "# get consequence that pass Bonferroni significance at least once \n",
    "vrnt_type_consq_pass_bonf = sv_vep_enrich_tests_df[sv_vep_enrich_tests_df.bonferroni_pass].vrnt_type_consequence.unique()\n",
    "sv_vep_enrich_tests_consq_pass_bonf_df = sv_vep_enrich_tests_df[sv_vep_enrich_tests_df.vrnt_type_consequence.isin(vrnt_type_consq_pass_bonf)].copy()\n",
    "# consequence names \n",
    "consq_names_dict = {\"non_coding_transcript_exon_variant\": \"Non-coding transcript\", \n",
    "                    \"upstream_gene_variant\": \"Upstream\",\n",
    "                    \"no_predicted_effect\": \"No predicted effect\",\n",
    "                    'transcript_amplification': \"Transcript amplification\", \n",
    "                    \"coding_sequence_variant\": \"Coding\"}\n",
    "sv_vep_enrich_tests_consq_pass_bonf_df[\"consq_name\"] = sv_vep_enrich_tests_consq_pass_bonf_df.consequence.replace(consq_names_dict)\n",
    "# mask any values not passing nominal cutoff \n",
    "sv_vep_enrich_tests_consq_pass_bonf_df[\"risk_ratio_pass\"] = np.where(sv_vep_enrich_tests_consq_pass_bonf_df.pval < 0.05, \n",
    "                                                                    sv_vep_enrich_tests_consq_pass_bonf_df.risk_ratio,\n",
    "                                                                     np.nan\n",
    "                                                                       )\n",
    "sv_vep_enrich_tests_consq_pass_bonf_df[\"risk_upper_pass\"] = np.where(sv_vep_enrich_tests_consq_pass_bonf_df.pval < 0.05,  \n",
    "                                                                        sv_vep_enrich_tests_consq_pass_bonf_df.risk_ratio_upper, \n",
    "                                                                        np.nan\n",
    "                                                                       )\n",
    "sv_vep_enrich_tests_consq_pass_bonf_df[\"risk_lower_pass\"] = np.where(sv_vep_enrich_tests_consq_pass_bonf_df.pval < 0.05,  \n",
    "                                                                        sv_vep_enrich_tests_consq_pass_bonf_df.risk_ratio_lower,\n",
    "                                                                         np.nan\n",
    "                                                                       )\n",
    "\n",
    "# write to file \n",
    "sv_consq_pass_bonf_path = subset_enrich_path.joinpath(\"sv_consq_pass_bonf.tsv\")\n",
    "sv_vep_enrich_tests_consq_pass_bonf_df.to_csv(sv_consq_pass_bonf_path, sep=\"\\t\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "66e22c28",
   "metadata": {},
   "outputs": [],
   "source": [
    "# write z-cutoff = 10 to file for plotting for main text figure \n",
    "sv_consq_pass_z10_bonf_df = sv_vep_enrich_tests_consq_pass_bonf_df[(sv_vep_enrich_tests_consq_pass_bonf_df.z_cutoff == 10)\n",
    "                                                                  ].copy()\n",
    "sv_consq_pass_bonf_path = subset_enrich_path.joinpath(\"sv_consq_pass_bonf_z10.tsv\")\n",
    "sv_consq_pass_z10_bonf_df.to_csv(sv_consq_pass_bonf_path, sep=\"\\t\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98ae1a70",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
